import util from '../util.js';
import pool from './db.js';

// 查询所有音乐
export async function getAllMusicPackages() {
    const [rows] = await pool.query('SELECT * FROM music_package');
    return rows;
}

// 根据ID查询音乐
export async function getMusicPackageById(id) {
    const [rows] = await pool.query('SELECT * FROM music_package WHERE id = ?', [id]);
    return rows[0];
}

// 添加音乐
export async function addMusicPackage(music_package) {
    const { name, description } = music_package;
    const now = util.getDatetime();
    const [result] = await pool.query(
        'INSERT INTO music_package (name, description, create_time, update_time) VALUES (?, ?, ?, ?)',
        [name, description, now, now]
    );
    return result.insertId;
}

// 更新音乐包
export async function updateMusicPackage(id, music_package) {
    const { name, description } = music_package;
    const now = new Date();
    const [result] = await pool.query(
        'UPDATE music_package SET name = ?, description = ?, update_time = ? WHERE id = ?',
        [name, description, now, id]
    );
    return result.affectedRows > 0;
}

// 删除音乐
export async function deleteMusicPackage(id) {
    const [result] = await pool.query('DELETE FROM music_package WHERE id = ?', [id]);
    return result.affectedRows > 0;
}